"""
Case Type   : 资源池化系统视图
Case Name   : 默认配置下，查看系统视图gs_thread_memory_context
Create At   : 2025/2
Owner       : haomeng
Description :
    1、主节点查看系统表gs_thread_memory_context中DMSMemContext字段是否存在
    2、备节点查看系统表gs_thread_memory_context中DMSMemContext字段是否存在
    3、查看enable_thread_pool参数值
    4、主节点查询相同系统视图gs_session_memory_detail中DMSMemContext字段是否存在
    5、备节点查询相同系统视图gs_session_memory_detail中DMSMemContext字段是否存在
Expect      :
    1、存在DMSMemContext相关字段及信息
    2、存在DMSMemContext相关字段及信息
    3、默认值为off
    4、存在DMSMemContext相关字段及信息
    5、存在DMSMemContext相关字段及信息
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class ShareStorageSystemView(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.prish = CommonSH('PrimaryDbUser')
        self.stash = CommonSH('Standby1DbUser')
        self.constant = Constant()

        self.thread_pool = 'enable_thread_pool'
        self.sys_view1 = 'gs_thread_memory_context'
        self.sys_view2 = 'gs_session_memory_detail'
        self.sys_view3 = 'gs_get_thread_memctx_detail'

    def test_ss_system_view(self):
        text = '-----step1:主节点查看系统表gs_thread_memory_context中DMSMemContext字段是否存在  ' \
               'expect:存在DMSMemContext相关字段及信息-----'
        self.log.info(text)

        sql1 = f"select * from {self.sys_view1} where contextname='DMSMemContext';"
        self.log.info(sql1)
        res1 = self.prish.execut_db_sql(sql1)
        self.log.info(res1)
        self.assertIn('(1 row)', res1, '存在异常信息')

        text = '-----step2:备节点查看系统表gs_thread_memory_context中DMSMemContext字段是否存在  ' \
               'expect:存在DMSMemContext相关字段及信息-----'
        self.log.info(text)

        sql2 = f"select * from {self.sys_view2} where contextname='DMSMemContext';"
        self.log.info(sql2)
        res2 = self.stash.execut_db_sql(sql2)
        self.log.info(res2)
        self.assertIn('(1 row)', res2, '存在异常信息')

        text = '-----step3:查看线程池开关;  expect:默认值为off-----'
        self.log.info(text)

        value = self.prish.execut_db_sql(f'show {self.thread_pool}')
        self.log.info(value)
        self.assertEqual('off', value.splitlines()[-2].strip(), '参数默认值校验失败')

        text = '-----step4:主节点查询相同系统视图gs_session_memory_detail中DMSMemContext字段是否存在  ' \
               'expect:存在DMSMemContext相关字段及信息-----'
        self.log.info(text)

        sql3 = f"select * from {self.sys_view1} where contextname='DMSMemContext';"
        self.log.info(sql3)
        res3 = self.prish.execut_db_sql(sql3)
        self.log.info(res3)
        self.assertIn('(1 row)', res3, '存在异常信息')

        tid1 = res3.splitlines()[-2].split('|')[0].split('.')[-1]
        self.log.info(tid1)
        res3_2 = self.prish.execut_db_sql(f"select * from {self.sys_view3}({tid1},'DMSMemContext');")
        self.log.info(res3_2)
        self.assertIn('ss_dms_callback.cpp', res3_2, '存在异常信息')

        text = '-----step5:备节点查询相同系统视图gs_session_memory_detail中DMSMemContext字段是否存在  ' \
               'expect:存在DMSMemContext相关字段及信息-----'
        self.log.info(text)

        sql4 = f"select * from {self.sys_view2} where contextname='DMSMemContext';"
        self.log.info(sql4)
        res4 = self.stash.execut_db_sql(sql4)
        self.log.info(res4)
        self.assertIn('(1 row)', res4, '存在异常信息')

        tid2 = res4.splitlines()[-2].split('|')[0].split('.')[-1]
        self.log.info(tid2)
        res4_2 = self.stash.execut_db_sql(f"select * from {self.sys_view3}({tid2},'DMSMemContext');")
        self.log.info(res4_2)
        self.assertIn('ss_dms_callback.cpp', res4_2, '存在异常信息')

    def tearDown(self):
        self.log.info('-----No need to restore environment-----')
        self.log.info(f'----{os.path.basename(__file__)} end----')
